
package main;

import main.SQL.MsSQLConnect;
import main.Trees.SmetNode;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class OpenSQLSmet {

    private SmetNode myroot;
    private Statement stat;
    private Connection connSql, connSqlIf;
    private ArrayList<SmetNode> smc, smt, smo, sms, importFactageNodes, smetNodes;
    private HashMap<Integer, SmetNode> EPSNode;

    public SmetNode open() {

        try {

            this.myroot = new SmetNode("Все сметы");
            this.connSql = MsSQLConnect.ConnectDB("TST");
            this.connSqlIf = MsSQLConnect.ConnectDB("ImportFactage");
            importFactageNodes = new ArrayList<>();

            getSmcList();
            getEPSList();
            getSmtList();
            getSmoList();
            getSmsList();
            getImportFactageSmet();

            addHierarchy();

            this.connSql.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return myroot;
    }

    private void getSmcList() {

        this.smc = new ArrayList<SmetNode>();

        try {

            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT complex_id, name FROM ks_Complex");

            while (rs.next()) {
                this.smc.add(new SmetNode(0, rs.getInt("complex_id"), rs.getString("name")));
            }

            this.smc.add(new SmetNode(0, 100500, "ImportFactage"));

            rs.close();
            stat.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void getSmtList() {

        this.smt = new ArrayList<>();

        try {

            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM ks_Tituls");

            while (rs.next()) {
                this.smt.add(new SmetNode(rs.getInt("complex_id"), rs.getInt("titul_id"), rs.getString("name")));
            }

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void getEPSList() {

        EPSNode = new HashMap<>();
        try {

            Statement statif = connSqlIf.createStatement();
            ResultSet rs = statif.executeQuery("SELECT * FROM EPS");

            while (rs.next()) {
                SmetNode newNode = new SmetNode(rs.getInt("parent_id"), rs.getInt("id"), rs.getString("name"));
                importFactageNodes.add(newNode);
                EPSNode.put(rs.getInt("id"), newNode);
            }

            rs.close();
            statif.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void getSmoList() {

        this.smo = new ArrayList<>();

        try {

            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM ks_Objects");

            while (rs.next())
                this.smo.add(new SmetNode(rs.getInt("titul_id"), rs.getInt("object_id"), rs.getString("name")));

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void getSmsList() {

        this.sms = new ArrayList<>();
        try {

            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM ks_Smeta WHERE smetIsSSR = 0");

            while (rs.next())
                this.sms.add(new SmetNode(rs.getInt("object_id"), rs.getInt("smet_id"), "Смета " + rs.getString("smetNumber") + " - " + rs.getString("smetName")));

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void getImportFactageSmet() {

        smetNodes = new ArrayList<>();
        try {

            stat = connSqlIf.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM Smet");

            while (rs.next()) {
                SmetNode newSmet = new SmetNode(rs.getInt("eps_id"), rs.getInt("id"), "Смета " + rs.getString("num") + " - " + rs.getString("name"));
                newSmet.setIsImportFactage(true);
                smetNodes.add(newSmet);
            }

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void addHierarchy() {

        for (SmetNode curSmc : smc) {
            this.myroot.add(curSmc);
            addSmtChildren(curSmc);
        }

    }

    private void addSmtChildren(SmetNode parent) {

        if (parent.getId() == 100500) {
            for (SmetNode nodes : importFactageNodes) {
                if (nodes.getId() > 0) {
                    if (nodes.getParentId() != 0) {
                        EPSNode.get(nodes.getParentId()).add(nodes);
                    } else {
                        parent.add(nodes);
                    }
                }
            }
            for (SmetNode smets : smetNodes) {
                smets.isString = true;
                smets.setAllowChildren(false);
                EPSNode.get(smets.getParentId()).add(smets);
            }
        } else {
            for (SmetNode node : smt) {

                if (node.getParentId() != parent.getId()) continue;
                parent.add(node);
                addSmoChildren(node);

            }
        }
    }

    private void addSmoChildren(SmetNode parent) {

        for (SmetNode node : smo) {
            if (node.getParentId() != parent.getId()) continue;
            parent.add(node);
            addSmsChildren(node);
        }
    }

    private void addSmsChildren(SmetNode parent) {

        for (SmetNode node : sms) {
            if (node.getParentId() != parent.getId()) continue;
            node.isString = true;
            node.setAllowChildren(false);
            parent.add(node);
        }
    }

}
